﻿using System;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace DotNetDetour
{
    public static class MethodBaseExtend
    {
        public static IntPtr GetRawPtr(this MethodBase method)
        {
            var clsType = method.DeclaringType;
            if (clsType.IsGenericType)
            {
                //Jit处理
                var typeHandles = clsType.GetGenericArguments().Select(t => t.TypeHandle).ToArray();
                RuntimeHelpers.PrepareMethod(method.MethodHandle, typeHandles);
            }
            else
            {
                RuntimeHelpers.PrepareMethod(method.MethodHandle);
            }
            return method.MethodHandle.GetFunctionPointer();
        }
    }
}